//weui.js是下拉框输出格式转成yyyy-MM-dd
var formatDate = function(res) {
	var y = res[0].value;
	var m = res[1].value;
	var d = res[2].value;

	if(m < 10) {
		m = '0' + m;
	}
	if(d < 10) {
		d = '0' + d;
	}
	return y + '-' + m + '-' + d;
};

//获取地址栏参数
var getUrlParam = function(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r != null) return decodeURIComponent(r[2]);
	return "";
};

//获取地址格式字符串参数
var getParam = function(url, name) {
	var reg = new RegExp("(^|&|\\?)" + name + "=([^&]*)(&|$)");
	var r = url.substr(1).match(reg);
	if(r != null) return decodeURIComponent(r[2]);
	return null;
};

//格式化时间小于10的部分
var formatTimeIfLessThanTen = function(i) {
	if(i < 10) {
		i = "0" + i;
	}
	return i;
};

//用下拉框类型获取所属的icon的路径
var getIconName = function(type) {
	//time,text,radio,checkbox,dropdown
	if(type === 'time') {
		return 'img/icon/calendar.png';
	} else if(type === 'dropdown') {
		return 'img/icon/xiala.png';
	}
	return '';
};

//获取今天的yyyy-MM-dd
var getDateTime = function(addDay) {
	var time = new Date(new Date().getTime());
	if(addDay !== null && addDay !== undefined) {
		time.setDate(time.getDate() + addDay);
	}
	var y = time.getFullYear();
	var m = time.getMonth() + 1;
	var d = time.getDate();
	console.log(y, m, d);
	return y + "-" + formatTimeIfLessThanTen(m) + "-" + formatTimeIfLessThanTen(d);
};

//获取今天+addDay的年月日 [yyyy,MM,dd]格式
var getCurrentDateArr = function(addDay) {
	var time = new Date(new Date().getTime());
	if(addDay !== null && addDay !== undefined) {
		time.setDate(time.getDate() + addDay);
	}
	var y = time.getFullYear();
	var m = time.getMonth() + 1;
	var d = time.getDate();
	console.log(y, m, d);
	return [y, m, d];
};

//获取今年+AddYear的年
var getYear = function(addYear) {
	var time = new Date(new Date().getTime());
	var y = time.getFullYear();
	//没参数的时候直接返回2015年
	if(addYear === undefined || addYear === null) {
		y = 2015;
	} else {
		//有参数的时候，在今年往后加 addYear 年
		y = y + addYear;
	}
	return y;
};

 

//获取今天+AddDay的天
var getDay = function(addDay) {
	var time = new Date(new Date().getTime());
	var d = time.getDate();
	if(addDay !== null && addDay !== undefined) {
		d = d + addDay;
	}
	return d;
};

String.prototype.trimAll = function(char, type) {
	if(char) {
		if(type == 'left') {
			return this.replace(new RegExp('^\\' + char + '+', 'g'), '');
		} else if(type == 'right') {
			return this.replace(new RegExp('\\' + char + '+$', 'g'), '');
		}
		return this.replace(new RegExp('^\\' + char + '+|\\' + char + '+$', 'g'), '');
	}
	return this.replace(/^\s+|\s+$/g, '');
};

//封装的alert
var layerAlert = function(msg) {
	layerEx.open({
		style: 'border:none; background-color:#fff; color:#000;',
		content: msg
	});
};

//封装带回调的alert
var layerAlertAndCallback = function(msg, doEnd) {
	layerEx.open({
		style: 'border:none; background-color:#fff; color:#000;',
		content: msg,
		end: function(index) {
			doEnd();
		}
	});
};